package Reservation;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Application.Dao.UserDAO;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		if (action.equals("login")) {
			String email = request.getParameter("email");
			String password = request.getParameter("pw");
			UserBean user = new UserBean();
			user.setEmail(email);
			user.setPassword(password);

			user = UserDAO.login(user);

			if (user.isValid()) {
				HttpSession session = request.getSession(true);
				session.setAttribute("currentSessionUser", user);
				// response.sendRedirect("CustomerPage.jsp");//logged-in page
				if(user.getIdentity()!=1)
				response.sendRedirect("NewCustomerPage.jsp");
				else
					response.sendRedirect("AdminIndex.jsp");
			} else
				response.sendRedirect("invalidLogin.jsp");
		} else if (action.equals("register")) {
			// get customers' information
			System.out.println("start to get");
			String firstname = request.getParameter("firstName");
			String lastname = request.getParameter("lastName");
			String street = request.getParameter("address");
			String city = request.getParameter("city");
			String state = request.getParameter("state");
			String zip = request.getParameter("zip");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String pw = request.getParameter("pw");
			// String rpw = request.getParameter("rpw");
			// int accnumber =
			// Integer.parseInt(request.getParameter("accnumber"));
			System.out.println("Success in get inf");

			// Create a new user with information get from registration page
			UserBean user = new UserBean();
			user.setFirstName(firstname);
			user.setLastName(lastname);
			user.setAddress(street + ", " + city + ", " + state + ", " + zip);
			user.setEmail(email);
			user.setPassword(pw);
			user.setPhone(phone);
			// user.setUserID(accnumber);
			user.setValid(true);

			// Insert user to the Database
			UserDAO.createUser(user);
			if (user.isValid()) {
				HttpSession session = request.getSession(true);
				session.setAttribute("currentSessionUser", user);
				response.sendRedirect("NewCustomerPage.jsp");// Customer registered
															// and log in
			} else
				response.sendRedirect("invalidLogin.jsp");
		} else if (action.equals("make_reservation")) {
			int people_num = (int) Integer.parseInt(request
					.getParameter("people_num"));
			String res_date = request.getParameter("datepicker");
			String from = request.getParameter("From");
			String to = request.getParameter("To");
			System.out.println("people_num" + people_num + "res_date "
					+ res_date + "from " + from + "to " + to);
			// response.sendRedirect("CustomerPage.jsp");
		}
		else if(action.equals("post"))
		{
			
		}
	}

}
